﻿using BenCaoXuan.Infrastructure.Attr;
using BenCaoXuan.Infrastructure.Dto;
using BenCaoXuan.Infrastructure.Dto.Admin.employee;
using BenCaoXuan.Infrastructure.Dto.PC;
using BenCaoXuan.Infrastructure.EFCore.MySql;
using BenCaoXuan.Infrastructure.IOC;
using BenCaoXuan.Infrastructure.Redis;
using BenCaoXuan.Infrastructure.Tools;
using BenCaoXuan.Service.Interface.PC;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BenCaoXuan.Service.Instance.PC
{
    [Inject_]
    public class Subscribe_Service : Base_Service, I_Subscribe_Service
    {
        private readonly Appointment_IOC _appointment_IOC;
        private readonly Product_IOC _product_IOC;

        public Subscribe_Service(Appointment_IOC appointment_IOC,Product_IOC product_IOC)
        {
            _appointment_IOC = appointment_IOC;
            _product_IOC=product_IOC; 
        }

        /// <summary>
        /// 获取所有预约数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Appointment(Sys_Dto dto)
        {
            // 预约表
            var data = await _appointment_IOC._appointment_EFCore.GetAll(out int total, dto.page, dto.limit, false, d => d.ATime, d=>d.ACustomerName.Contains(dto.key) || d.APhone.Contains(dto.key)).Select(d => new Appointment_Dto
            {
                id = d.AId,
                name = d.ACustomerName,
                p_sid = d.AProductId, // 产品规格id 逗号隔开
                p_pid = d.APackageId, // 套餐id 逗号隔开
                c_phone = d.APhone,
                time = d.ATime,
                period = d.APeriod.Value.ToString("yyyy-MM-dd"),
            }).ToListAsync();

            // 套餐表
            var package = await _product_IOC._productPackage_EFCore.GetAll().Select(d => new Product_Package_Dto
            {
                id = d.PpId,
                name = d.PpName,
            }).ToListAsync();

            // 产品表
            var product = await _product_IOC._product_EFCore.GetAll().Select(d => new Product_Dto
            {
                id = d.PId, // 产品id
                name = d.PName, // 产品名字
            }).ToListAsync();

            // 产品规格表
            var product_detail = await _product_IOC._productSpecs_EFCore.GetAll().Select(d => new Product_Specs_Dto
            {
                p_sid = d.PsId, // 规格id
                name = d.PsName, // 规格名字
                p_id = d.PsProductId, // 产品id
            }).ToListAsync();

            // 构造最终返回的数据
            var result = data.Select(appointment => new
            {
                id = appointment.id,
                name = appointment.name,
                phone = appointment.c_phone,
                time = appointment.time,
                period = appointment.period,
                services = GetServices(appointment.p_sid, appointment.p_pid, product_detail, product, package)
            }).ToList();

            return Get_Result(1, "ok", new
            {
                total = total,
                data = result,
            });
        }

        /// <summary>
        /// 预约详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Appointment_Detail(string id)
        {
            var data=await _appointment_IOC._appointment_EFCore.GetAll(d=>d.AId==id).Select(d=>new Appointment_Dto
            {
                id=d.AId,
                name=d.ACustomerName,
                c_phone=d.APhone,
                Isorder=d.AIsOrder,
                money=d.AMoney.ToString(),
                p_pid=d.APackageId,
                p_sid=d.AProductId,
                period=d.APeriod.Value.ToString("yyyy-MM-dd"),
                status=d.AStatus,
                time=d.ATime,
                visit=d.AVisit,
            }).ToListAsync();

            // 套餐表
            var package =await _product_IOC._productPackage_EFCore.GetAll().Select(d => new Product_Package_Dto
            {
                id = d.PpId,
                name = d.PpName,
            }).ToListAsync();

            // 产品表
            var product = await _product_IOC._product_EFCore.GetAll().Select(d => new Product_Dto
            {
                id = d.PId, // 产品id
                name = d.PName, // 产品名字
            }).ToListAsync();

            // 产品规格表
            var product_detail = await _product_IOC._productSpecs_EFCore.GetAll().Select(d => new Product_Specs_Dto
            {
                p_sid = d.PsId, // 规格id
                name = d.PsName, // 规格名字
                p_id = d.PsProductId, // 产品id
            }).ToListAsync();

            // 构造最终返回的数据
            var result = data.Select(appointment => new
            {
                id = appointment.id,
                name = appointment.name,
                phone = appointment.c_phone,
                time = appointment.time,
                period = appointment.period,
                Isorder=appointment.Isorder,
                money = appointment.money,
                status = appointment.status,
                visit = appointment.visit,
                services = GetServices(appointment.p_sid, appointment.p_pid, product_detail, product, package)
            }).SingleOrDefault();

            return Get_Result(1, "ok", result);
        }

        /// <summary>
        /// 获取选中日期的所有预约数据
        /// </summary>
        /// <param name="date"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Time_Appointment(string time, Sys_Dto dto)
        {
            // 预约表
            var data = await _appointment_IOC._appointment_EFCore.GetAll(out int total, dto.page, dto.limit, false, d => d.ATime, d => d.APeriod== Convert.ToDateTime(time) && (d.ACustomerName.Contains(dto.key) || d.APhone.Contains(dto.key))).Select(d => new Appointment_Dto
            {
                id = d.AId,
                name = d.ACustomerName,
                p_sid = d.AProductId, // 产品规格id 逗号隔开
                p_pid = d.APackageId, // 套餐id 逗号隔开
                c_phone = d.APhone,
                time = d.ATime,
                period = d.APeriod.Value.ToString("yyyy-MM-dd"),
            }).ToListAsync();

            // 套餐表
            var package = await _product_IOC._productPackage_EFCore.GetAll().Select(d => new Product_Package_Dto
            {
                id = d.PpId,
                name = d.PpName,
            }).ToListAsync();

            // 产品表
            var product = await _product_IOC._product_EFCore.GetAll().Select(d => new Product_Dto
            {
                id = d.PId, // 产品id
                name = d.PName, // 产品名字
            }).ToListAsync();

            // 产品规格表
            var product_detail = await _product_IOC._productSpecs_EFCore.GetAll().Select(d => new Product_Specs_Dto
            {
                p_sid = d.PsId, // 规格id
                name = d.PsName, // 规格名字
                p_id = d.PsProductId, // 产品id
            }).ToListAsync();

            // 构造最终返回的数据
            var result = data.Select(appointment => new
            {
                id = appointment.id,
                name = appointment.name,
                phone = appointment.c_phone,
                time = appointment.time,
                period = appointment.period,
                services = GetServices(appointment.p_sid, appointment.p_pid, product_detail, product, package)
            }).ToList();

            return Get_Result(1, "ok", new
            {
                total=total,
                data=result,
            });
        }

        private string GetServices(string p_sid, string p_pid, List<Product_Specs_Dto> product_detail, List<Product_Dto> product, List<Product_Package_Dto> package)
        {
            var services = new List<string>();

            // 处理产品规格
            if (!string.IsNullOrEmpty(p_sid))
            {
                var productIds = p_sid.Split(',').Select(id => id).ToList();
                var productSpecs = product_detail.Where(pd => productIds.Contains(pd.p_sid)).ToList();

                foreach (var spec in productSpecs)
                {
                    var productName = product.FirstOrDefault(p => p.id == spec.p_id)?.name;
                    if (productName != null)
                    {
                        services.Add($"{productName}({spec.name})");
                    }
                }
            }

            // 处理套餐
            if (!string.IsNullOrEmpty(p_pid))
            {
                var packageIds = p_pid.Split(',').Select(id =>id).ToList();
                var packages = package.Where(p => packageIds.Contains(p.id)).ToList();

                foreach (var pkg in packages)
                {
                    services.Add(pkg.name);
                }
            }

            return string.Join(" + ", services);
        }
    }
}
